package final2107.night.day07;

public class DeadLock {
    public static void main(String[] args) {
        //创建2个线程对象并发执行
        User user1 = new User();
        User user2 = new User();
        MyThread thread1 = new MyThread(user1,user2);
        MyThread thread2 = new MyThread(user2,user1);
        
        thread1.start();
        thread2.start();
    }
}

class MyThread extends Thread{
    private User user1;  //该变量保存第一个加锁的对象
    private User user2;  //该变量保存第二个加锁的对象

    MyThread(User user1,User user2){
        this.user1 = user1;
        this.user2 = user2;
    }

    @Override
    public void run() {
        //给第一个对象加锁
        synchronized (user1){
            System.out.println(Thread.currentThread().getName()+"执行第一步操作");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //给第二个对象加锁
            synchronized (user2){
                System.out.println(Thread.currentThread().getName()+"执行第二步操作");
            }
        }

    }
}

class User{}
